package com.hsr.web.service.impl;

import com.hsr.web.entity.TUser;
import com.hsr.web.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tb
 * @create 2022-04-03 21:49
 * @remark
 */
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    ITUserService itUserService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (username == null || "".equals(username)) {
            throw new RuntimeException("用户不能为空");
        }
        //根据用户名查询用户
        TUser user = itUserService.getById("1");
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        //直接赋予admin权限
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("admin");
        grantedAuthorities.add(grantedAuthority);
        return new User(user.getUserNum(), user.getUserPwd(), true, true, true, true, grantedAuthorities);
    }
}
